//
//  moduleCollection.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//
import Foundation
import UIKit
import SnapKit
import Alamofire
class ModuleCollection:UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    //全局变量
    private var ModuleInfoItems:[ModuleInfo]=[]
    private let api = "/modulelist"
    //初始化方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        attributeInit()
    }
    convenience init(frame: CGRect) {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 20, left: 6, bottom: 0, right: 6)
        self.init(frame: frame, collectionViewLayout: layout)
        loadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //属性配置
    func attributeInit(){
        backgroundColor = .white
        layer.cornerRadius = 15
        
        dataSource = self
        delegate = self
        register(ModuleItemCell.self, forCellWithReuseIdentifier: "ModuleItemCell")
        register(MyHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MyHeader")
        
        
    }
    func setupUI(){
        
    }
    
    
    func loadData(){
        let url = AllUrl.LH_URL + api
        AF.request(url,method: .get)
            .validate(statusCode: 200..<300)
            .responseDecodable(of:[ModuleInfo].self){ response in
                switch response.result{
                case .success(let moduleList):
                    DispatchQueue.main.async {
                        self.ModuleInfoItems = moduleList
                        self.reloadData()
                    }
                case .failure(let error):
                    print("错误: \(error.localizedDescription)")
                }
            }
    }
    
    //collection函数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ModuleInfoItems.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ModuleItemCell", for: indexPath)as! ModuleItemCell
        let  item = ModuleInfoItems[indexPath.row]
        cell.configure(with: item)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            let header = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: "MyHeader",
                for: indexPath)as! MyHeaderView
            return header
        }
        return UICollectionReusableView()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 54, height: 45);
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 0)
    }
}
